package com.isoft.systemprovidor.service.approval;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.systemprovidor.bean.ResponseData;
import com.isoft.systemprovidor.bean.ResultCode;
import com.isoft.systemprovidor.dao.approval.Approval_DRP_Dao;
import com.isoft.systemprovidor.entity.approval.Approval_DRP;
import com.isoft.systemprovidor.entity.approval.Approval_PRP;
import com.isoft.systemprovidor.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * (9)	日常报销待审批单：费用报销单、贴票据、审批记录历史、审批意见、个人签名。
 * 	【费用报销单】所显示的信息：填报日期、部门、姓名、报销事由、报销金额（小写、大写）。
 * 	【贴票据】所显示的信息：上传报销用的各种票据图片。
 * 	【审批历史记录】显示信息项：审批步骤、审批人、操作、审批意见、操作时间。
 * 	【审批意见】显示信息项：审批意见。
 * 	【个人签名】显示信息项：领导个人的电子签名。
 * **/
@Service
@Slf4j
public class Approval_DRP_Service {
    @Autowired
    Approval_DRP_Dao drpDao;
    /*审核人员操作*/
    /*分页获取审批信息*/
    public ResponseData getallPage(Integer currentPage, Integer size , Integer appoperate, String name){

        Page<Approval_DRP> Page = new Page<>();//*
        // 初始化分页参数
        if (currentPage == null || currentPage<1){
            currentPage = 1;
        }
        if (size == null || size<1){
            size =10;
        }
        // 构建查询条件
        Page.setCurrent(currentPage).setSize(size);
        QueryWrapper wrapper = new QueryWrapper();
        if (!name.isEmpty()){
            wrapper.like("name" , name);//*
        }
        if (!appoperate.equals(100)){//客户端获取全部信息时 操作码为100
            wrapper.eq("appoperate" ,appoperate);
        }
        wrapper.orderByDesc("operatetime");
        //构造返回Controller 的结果
        ResponseData responseData = new ResponseData();
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(drpDao.selectPage(Page,wrapper));//*
        return responseData;
    }
    /*办理审核业务*/
    public ResponseData auditService(Approval_DRP approval){//*
        ResponseData responseData = new ResponseData();
        if ((null == approval)||(!IdUtil.idCheck(approval.getId()))){
            log.info("参数错误");
            responseData.setErrorCode(ResultCode.PARAM_IS_BLANK.getCode());
            responseData.setMsg(ResultCode.PARAM_IS_BLANK.getMessage());
            return responseData;
        }
        //获取操作时间
        approval.setOperatetime(new Date());
        //构造持久化条件
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("id" ,approval.getId());
        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(drpDao.update(approval,wrapper));
        log.info("持久化成功");
        return responseData;

    }

    /*分页查询我的已办*/
    public ResponseData getPageMyDone(Integer currentPage, Integer size,String audituserid){
        Page<Approval_DRP> Page = new Page<>();//*、
        ResponseData responseData = new ResponseData();
        if (audituserid.isEmpty()){
            log.info("审核人员参数为空");
            responseData.setErrorCode(ResultCode.PARAM_IS_BLANK.getCode());
            responseData.setMsg(ResultCode.PARAM_IS_BLANK.getMessage());
            return responseData;
        }
        // 初始化分页参数
        if (currentPage == null){
            currentPage = 1;
        }
        if (size == null){
            size =10;
        }
        // 构建查询条件
        Page.setCurrent(currentPage).setSize(size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("audituserid" ,audituserid);
        wrapper.orderByDesc("operatetime");

        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(drpDao.selectPage(Page,wrapper));//*
        return responseData;
    }

}
